package com.health.controller;

import com.health.pojo.ExamineOrder;
import com.health.pojo.Users;
import com.health.service.HealthComboService;
import com.sun.corba.se.impl.orbutil.ObjectUtility;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(value = "/bookCombo")
public class BookComboServlet extends HttpServlet {
    HealthComboService healthComboService = new HealthComboService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取套餐id和预约时间
        String examineId = request.getParameter("medicalPlanId");
        String examineDate = request.getParameter("bookTime");
        // 如果点击按钮是数据空则跳转到显示下拉框项控制方法
        if ("".equals(examineDate)) {
            request.setAttribute("asideStatus", 2);
            response.sendRedirect(request.getContextPath() + "/medicalSearch");
            return;
        }
        // 接收session的用户id参数
        Integer userId = ((Users) request.getSession().getAttribute("user")).getUserId();
        // 执行添加到表中方法
        ExamineOrder examineOrder = new ExamineOrder();
        examineOrder.setExamineId(Integer.parseInt(examineId));
        String replaceDate = examineDate.replace("-", "");
        examineOrder.setExamineDate(replaceDate);  // 解决转义字符问题
        boolean BookStatus = healthComboService.bookCombo(examineOrder, userId);
        // 设置响应编码
        response.setContentType("text/html;charset=utf-8");
        // 设置侧边栏状态
        request.setAttribute("asideStatus", 2);
        request.setAttribute("bookStatus", BookStatus);
        request.getRequestDispatcher("/restrictedPages/main.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
